//
//  GoodsDetailView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/27.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "GoodsDetailView.h"



@implementation GoodsDetailView
{
    UIScrollView  *_scrollView;
    UIPageControl *_pageControl;
    
    UILabel       *_page;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        [self setScrollView:_model];
    }
    return self;
}

#define scrollHeight SCREEN_WIDTH


- (void)setScrollView:(GoodsDetailModel *)model{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, scrollHeight)];
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 5, scrollHeight);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor clearColor];
    [self addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 50, scrollHeight - 30, 100, 30)];
    _pageControl.numberOfPages = model.ImageUrlArray.count;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = APPCOLOR;
    _pageControl.currentPage = 0;
    [self addSubview:_pageControl];
    
    for (int i = 0; i < _pageControl.numberOfPages + 1; i++) {
        int temp = i;
        if (i == _pageControl.numberOfPages) {
            temp = 0;
        }
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, scrollHeight)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.ImageUrlArray[temp]]];
        [_scrollView addSubview:imageView];
    }
    
    UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 45, scrollHeight - 45, 40, 40)];
    pageView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    pageView.layer.cornerRadius = 20;
    pageView.layer.masksToBounds = YES;
    [self addSubview:pageView];
    
    //页数
    _page = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, 35, 35)];
    _page.text = [NSString stringWithFormat:@"1 / %ld", _pageControl.numberOfPages];
    _page.textColor = [UIColor whiteColor];
    _page.textAlignment = NSTextAlignmentCenter;
    _page.font = [UIFont systemFontOfSize:13];
    [pageView addSubview:_page];
    
    //商品名
    UILabel *goodsName = [[UILabel alloc] initWithFrame:CGRectMake(8, scrollHeight + 10, SCREEN_WIDTH - 16, 0)];
    NSMutableAttributedString *attributeGoodsName = [[NSMutableAttributedString alloc] initWithString:model.Title];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    [paraStyle setLineSpacing:6];
    [attributeGoodsName addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [attributeGoodsName length])];
    goodsName.font = [UIFont systemFontOfSize:16];
    goodsName.numberOfLines = 0;
    goodsName.attributedText = attributeGoodsName;
    [goodsName sizeToFit];
    [self addSubview:goodsName];
    
    //商品价格
    UILabel *price = [[UILabel alloc] initWithFrame:CGRectMake(8, goodsName.frame.size.height + scrollHeight + 10 + 15, SCREEN_WIDTH - 16, 20)];
    NSMutableAttributedString *attributePrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@", model.Price]];
    [attributePrice addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:16.0]
                          range:NSMakeRange(0, attributePrice.length)];
    [attributePrice addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:13.0]
                          range:NSMakeRange(0, 1)];
    price.attributedText = attributePrice;
    price.textColor = APPCOLOR;
    [self addSubview:price];
    
    
    //分割线
    UIView *line0 = [[UIView alloc] initWithFrame:CGRectMake(0, price.frame.origin.y + 30, SCREEN_WIDTH, 8)];
    line0.backgroundColor = BackgroundCOLOR;
    [self addSubview:line0];
    
    //选择规格Button
    UIButton *chooseStandardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseStandardBtn.frame = CGRectMake(0, line0.frame.origin.y + 8, SCREEN_WIDTH, 44);
    [chooseStandardBtn addTarget:self action:@selector(chooseStandard) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:chooseStandardBtn];
    
    //选择规格label
    UILabel *chooseStandardLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 12, SCREEN_WIDTH - 16, 20)];
    chooseStandardLabel.text = @"选择规格";
    chooseStandardLabel.font = [UIFont systemFontOfSize:16];
    [chooseStandardBtn addSubview:chooseStandardLabel];
    
    //选择规格 折角
    UIImageView *indicator = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 15, 16, 7, 12)];
    indicator.image = [UIImage imageNamed:@"XQYicon_22"];
    [chooseStandardBtn addSubview:indicator];
    
    //分割线
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, chooseStandardBtn.frame.origin.y + 44, SCREEN_WIDTH, 8)];
    line1.backgroundColor = BackgroundCOLOR;
    [self addSubview:line1];
    
    //商品描述label
    UILabel *goodsDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, line1.frame.origin.y + 18, SCREEN_WIDTH - 16, 21)];
    goodsDetailLabel.font = [UIFont systemFontOfSize:16];
    goodsDetailLabel.text = @"商品描述";
    [self addSubview:goodsDetailLabel];
    
    //商品描述
    UILabel *goodsDetail = [[UILabel alloc] initWithFrame:CGRectMake(8, goodsDetailLabel.frame.origin.y + 31, SCREEN_WIDTH - 16, 1000)];
    NSMutableAttributedString *attributeGoodsDetail = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"品牌: %@", model.BrandName]];
    [attributeGoodsDetail addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [attributeGoodsDetail length])];
    goodsDetail.font = [UIFont systemFontOfSize:16];
    goodsDetail.numberOfLines = 0;
    goodsDetail.attributedText = attributeGoodsDetail;
    goodsDetail.textColor = [UIColor lightGrayColor];
    [goodsDetail sizeToFit];
    [self addSubview:goodsDetail];
    
    //分割线
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, goodsDetail.frame.origin.y + goodsDetail.frame.size.height + 10, SCREEN_WIDTH, 8)];
    line2.backgroundColor = BackgroundCOLOR;
    [self addSubview:line2];
    
    _height = line2.frame.origin.y + 8;
}
//滚动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger pageIndex = (scrollView.contentOffset.x + SCREEN_WIDTH / 2) / SCREEN_WIDTH;
    if (scrollView.contentOffset.x == _pageControl.numberOfPages * SCREEN_WIDTH) {
        pageIndex = 0;
        scrollView.contentOffset = CGPointZero;
    }
    if (scrollView.contentOffset.x < 0) {
        pageIndex = _pageControl.numberOfPages - 1;
        scrollView.contentOffset = CGPointMake(_pageControl.numberOfPages * SCREEN_WIDTH + scrollView.contentOffset.x, 0);
    }
    if (scrollView.contentOffset.x > (_pageControl.numberOfPages - 1) * SCREEN_WIDTH && scrollView.contentOffset.x < (_pageControl.numberOfPages - 0.5) * SCREEN_WIDTH) {
        pageIndex = _pageControl.numberOfPages - 1;
    }else if (scrollView.contentOffset.x >= (_pageControl.numberOfPages - 0.5) * SCREEN_WIDTH){
        pageIndex = 0;
    }
    _pageControl.currentPage = pageIndex;
    
    //显示当前图片页
    _page.text = [NSString stringWithFormat:@"%ld / %ld", pageIndex + 1, _pageControl.numberOfPages];
}

- (void)chooseStandard{
    _standardClick();
}


@end
